package easynotes.swingui.dyncom;

import easynotes.concerns.Configuration;
import easynotes.concerns.Links;
import easynotes.concerns.NoteAdding;
import easynotes.concerns.NoteEditing;
import easynotes.concerns.NotesPresentation;
import easynotes.concerns.TODO;
import easynotes.concerns.UI;
import easynotes.concerns.WorkingWithFiles;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Milan
 */
@Links
@NotesPresentation(task = NotesPresentation.Task.LINKS)
@NoteEditing @NoteAdding
@UI
@TODO("Finish opening files in pdf reader")
public class EditLinksPanel extends javax.swing.JPanel implements LinksPanel {

    @WorkingWithFiles
    private final static JFileChooser fc = new JFileChooser();
    @Configuration
    private final static Set<String> supportedExts = new HashSet<>();

    static {
        supportedExts.add(".pdf");
        supportedExts.add(".html");

        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setCurrentDirectory(new File("."));
        fc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }
                int index = f.getAbsolutePath().lastIndexOf(".");
                if (index != -1) {
                    String extension = f.getAbsolutePath().substring(index);
                    if (supportedExts.contains(extension)) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public String getDescription() {
                return "documents";
            }
        });
    }

    /**
     * Creates new form EditLinksPanel
     */
    public EditLinksPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        link = new javax.swing.JTextField();
        openButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();

        openButton.setText("Open");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(link, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(openButton)
                .addComponent(searchButton))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.link.setText(getRelativePath(fc.getSelectedFile()));
        }
    }//GEN-LAST:event_searchButtonActionPerformed

        @WorkingWithFiles
        private String getRelativePath(File file) {
            String path = file.getAbsolutePath();
            String currentPath = (new File("")).getAbsolutePath();
            if (path.startsWith(currentPath)) {
                path = path.replace(currentPath + System.getProperty("file.separator"), "");
            }
            return path;
        }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField link;
    private javax.swing.JButton openButton;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setLink(String link) {
        this.link.setText(link);
    }

    @Override
    public String getLink() {
        return this.link.getText();
    }

    @Override
    public JComponent asJComponent() {
        return this;
    }

    public static class EditLinksPanelFactory implements PanelsFactory<EditLinksPanel> {

        @Override
        public EditLinksPanel getNewInstance() {
            return new EditLinksPanel();
        }

        public static EditLinksPanel.EditLinksPanelFactory getInstance() {
            return new EditLinksPanel.EditLinksPanelFactory();
        }
    }
}
